package com.sl.mes.app.controller;

import com.sl.mes.app.dao.agg.AggPdWkListVo;
import com.sl.mes.app.dao.agg.AggPdWkVo;
import com.sl.mes.core.util.ResponseUtil;
import com.sl.mes.db.itf.bd.IBdDefdoc;
import com.sl.mes.db.itf.bd.IBdMaterial;
import com.sl.mes.db.itf.bd.IBdWk;
import com.sl.mes.db.itf.pd.IPdWk;
import com.sl.mes.db.itf.pd.IPdWkProdinv;
import com.sl.mes.db.service.pd.PdWkProdinvService;
import com.sl.mes.db.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mes/bdwk")
@Validated
public class MesPdWkController {
    private static final Logger logger = LoggerFactory.getLogger(MesAuthController.class);
    @Resource
    private IBdWk iBdWkService;
    @Resource
    private IPdWk iPdWkService;
    @Resource
    private IPdWkProdinv iPdWkProdinvService;
    @Resource
    private IBdDefdoc iBdDefdocService;
    @Resource
    private IBdMaterial iBdMaterialService;

    /**
     * 工作中心分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAll")
    public List<BdWk> queryAll(Integer pageNo, Integer pageSize){
        return iBdWkService.queryAll(pageNo, pageSize);
    }

    /**
     * 根据cwkid工作中心(炉台)分类并分页
     * @param cwkid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAllByCwkid")
    public List<BdWk> queryAllByCwkid(String cwkid, Integer pageNo, Integer pageSize){
        List<BdWk> bdWkList = null;
        try {
            bdWkList = iBdWkService.queayAllByCwkId(cwkid, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            logger.info("参数传入不正确");
        }
        return bdWkList;
    }

    /**
     * 工作中心生产档案(包括生产产线物料和炉台的基本信息)(初投)
     * @param cpdwkid
     * @return
     */
    @GetMapping("/queryById")
    public AggPdWkVo queryById(String cpdwkid){
        AggPdWkVo pWkVo=new AggPdWkVo();
        if(cpdwkid!=null&&iPdWkService.findById(cpdwkid)!=null){
            PdWk pdWk=iPdWkService.findById(cpdwkid);
            BdWk bdWk=iBdWkService.findById(pdWk.getCwkid());
            PdWkProdinv pdWkProdinv=iPdWkProdinvService.findById(pdWk.getCpdwkid());
            BdDefdoc bdDefdoc=iBdDefdocService.findById(pdWk.getPkDefdoc());
            BdMaterial material=iBdMaterialService.findById(pdWk.getPkMaterial());
            //热场尺寸下拉选项
            List<BdDefdoc> lstBdDefdoc=iBdDefdocService.selectDrop();
            //坩埚型号下拉选项
            List<BdMaterial> lstMaterial=iBdMaterialService.selectDropType();

            pWkVo.setPdWk(pdWk);
            pWkVo.setBdWk(bdWk);
            pWkVo.setPdWkProdinv(pdWkProdinv);
            pWkVo.setBdDefdoc(bdDefdoc);
            pWkVo.setBdMaterial(material);
            pWkVo.setBdDefdocList(lstBdDefdoc);
            pWkVo.setBdMaterialList(lstMaterial);
        }
       return pWkVo;
    }

    /**
     * 根据主键查询复投
     * @param cpdwkid
     * @return
     */
    @GetMapping("/repeatQueryById")
    public AggPdWkVo repeatQueryById(String cpdwkid){
        AggPdWkVo pWkVo=new AggPdWkVo();
        if(cpdwkid!=null&&iPdWkService.findById(cpdwkid)!=null){
            PdWk pdWk=iPdWkService.findById(cpdwkid);
            BdWk bdWk=iBdWkService.findById(pdWk.getCwkid());
            PdWkProdinv pdWkProdinv=iPdWkProdinvService.findById(pdWk.getCpdwkid());
            pWkVo.setPdWkProdinv(pdWkProdinv);
            pWkVo.setPdWk(pdWk);
            pWkVo.setBdWk(bdWk);
        }
        return pWkVo;
    }


    /**
     * 根据批次查询
     * @param vdef3
     * @return
     */
    @GetMapping("/prepareQueryByVdef3")
    public Object repeatQueryByVdef3(String vdef3){
        Map<Object,Object>  result = new HashMap<>();
        AggPdWkVo pWkVo=new AggPdWkVo();
        if(vdef3!=null&&iPdWkService.findByVdef3(vdef3)!=null){
            PdWk pdWk=iPdWkService.findByVdef3(vdef3);
            BdWk bdWk=iBdWkService.findById(pdWk.getCwkid());
            PdWkProdinv pdWkProdinv=iPdWkProdinvService.findById(pdWk.getCpdwkid());
            pWkVo.setPdWkProdinv(pdWkProdinv);
            pWkVo.setPdWk(pdWk);
            pWkVo.setBdWk(bdWk);
        }else {
         return  ResponseUtil.fail(-1,"批次不存在");
        }
        result.put("pWkVo",pWkVo);
        return ResponseUtil.ok(result);
    }

    /**
     * 备料
     * @return
     */
    @GetMapping("/prepareQuery")
    public AggPdWkListVo repeatQuery(){
        AggPdWkListVo pWkListVo=new AggPdWkListVo();
        List<PdWk> pdWkList=iPdWkService.repeatWQuery();
        List<BdWk> bdWkList=iBdWkService.repeatWQuery();
        List<PdWkProdinv> pdWkProdinvList=iPdWkProdinvService.repeatWQuery();
        pWkListVo.setPdWkList(pdWkList);
        pWkListVo.setBdWkList(bdWkList);
        pWkListVo.setPdWkProdinvList(pdWkProdinvList);
        return pWkListVo;
    }

    /**
     * 收料
     * @param cwkid
     * @return
     */
    @GetMapping("/receiveQuery")
    public AggPdWkVo receiveQuery(String cwkid){
        AggPdWkVo aggPdWkVo=new AggPdWkVo();
        BdWk bdWk=iBdWkService.findById(cwkid);
        PdWk pdWk=iPdWkService.findByCwkid(cwkid);
        aggPdWkVo.setPdWk(pdWk);
        aggPdWkVo.setBdWk(bdWk);
        return aggPdWkVo;
    }

}
